﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tools;

namespace WPF数据采集系统.View
{
    /// <summary>
    /// HistoryView.xaml 的交互逻辑
    /// </summary>
    public partial class HistoryView : UserControl
    {
        public HistoryView()
        {
            InitializeComponent();
        }
        private DataTable TesterDataTable;
        private DataTable RunDataTable;
        private void dataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            //判断权限
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string s_date = History_StartDate.Text;
            string e_date = History_EndDate.Text;
            if (s_date == "" || e_date == "")
            {
                string CustomTitle = "日期选择异常";
                string CustomMsg = "日期范围选择不能为空！请确认！";
                if (MainWindow.LANGEUAGE != "zh-cn")
                {
                    CustomTitle = "Date selection exception";
                    CustomMsg = "Date range selection cannot be blank! Please confirm!";
                }
                CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Error);
                return;
            }
            string sql = "select * from TestResult where 日期 >= '" + s_date + "' and 日期<='" + e_date + "'";
            if (TesterDataTable != null)
            {
                TesterDataTable.Clear();
            }
            TesterDataTable = SqlHelper.GetDataSet(sql).Tables[0];
            dataGrid.ItemsSource = TesterDataTable.DefaultView;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string CustomTitle = "权限不足";
            string CustomMsg = "用户:" + MainWindow.LoginUser + " 角色:" + MainWindow.LoginRole + "无权进行该操作";
            //判断是否管理员
            if (MainWindow.LoginUser != "admin")
            {
                if (MainWindow.LANGEUAGE != "zh-cn")
                {
                    CustomTitle = "Insufficient permissions";
                    CustomMsg = "User: " + MainWindow.LoginUser + " Role: " + MainWindow.LoginRole + " does not have permission to perform this operation";
                }
                CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Error);
                return;
            }
            CustomTitle = "警告";
            CustomMsg = "删除操作不可逆,是否确认要删除所选记录？";
            if (MainWindow.LANGEUAGE == "zh-cn")
            {
                if (CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Error).IsYes)
                {
                    string s_date = History_StartDate.Text.Trim();
                    string e_date = History_EndDate.Text.Trim();
                    if (string.IsNullOrWhiteSpace(s_date) || string.IsNullOrWhiteSpace(e_date))
                    {
                        CustomTitle = "错误";
                        CustomMsg = "日期范围选择不能为空！请确认！";
                        CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Error);
                        return;
                    }
                    string sql = "Delete TestResult where 日期 >= '" + s_date + "' and 日期<='" + e_date + "'";
                    SqlHelper.Update(sql);
                    CustomTitle = "操作成功";
                    CustomMsg = "删除操作已成功执行！";
                    CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Success);
                    LoadDataGrid();
                }
            }
            else
            {
                CustomTitle = "Warning";
                CustomMsg = "Deletion is irreversible. Are you sure you want to delete the selected record?";
                if (CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Error).IsYes)
                {
                    string s_date = History_StartDate.Text.Trim();
                    string e_date = History_EndDate.Text.Trim();
                    if (string.IsNullOrWhiteSpace(s_date) || string.IsNullOrWhiteSpace(e_date))
                    {
                        CustomTitle = "Error";
                        CustomMsg = "Date range selection cannot be blank! Please confirm!";
                        CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Error);
                        return;
                    }
                    string sql = "Delete TestResult where 日期 >= '" + s_date + "' and 日期<='" + e_date + "'";
                    SqlHelper.Update(sql);
                    CustomTitle = "Operation successful";
                    CustomMsg = "Delete operation has been successfully executed!";
                    CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Success);
                    LoadDataGrid();
                }
            }

        }
        private void LoadDataGrid()
        {
            try
            {
                string sql1 = "select top 100 * from TestResult order by 日期 desc";
                if (TesterDataTable != null)
                {
                    TesterDataTable.Clear();
                }
                TesterDataTable = SqlHelper.GetDataSet(sql1).Tables[0];
                dataGrid.ItemsSource = TesterDataTable.DefaultView;

                string sql2 = "select top 100 * from RunData order by 日期 desc";
                if (RunDataTable != null)
                {
                    RunDataTable.Clear();
                }
                RunDataTable = SqlHelper.GetDataSet(sql2).Tables[0];
                dataGrid_log.ItemsSource = RunDataTable.DefaultView;
            }
            catch (Exception e)
            {
                CustomMsgBox.ShowDialog("ERROR", e.Message, CustomMsgBoxButton.One, CustomMsgBoxImage.Error);
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            SaveFileDialog exe = new SaveFileDialog();
            exe.Filter = " xlsx(*.xlsx)|*.xlsx| xls(*.xls)|*.xls";
            exe.FilterIndex = 0;
            exe.RestoreDirectory = true;
            //exe.CreatePrompt = true;
            exe.Title = "Export Excel File";
            exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            exe.FileName = string.Format("{0}_{1}", DateTime.Now.ToString("yyyy-MM-dd_HH时mm分ss秒"), "导出");
            bool exe_result = (bool)exe.ShowDialog();
            if (exe_result)
            {
                string path = exe.FileName;
                string result = Export.ToExcel(RunDataTable, path);//TesterDataTable
                if (result == "success")
                {
                    CustomMsgBox.ShowDialog("导出数据成功", "导出目录:" + path, CustomMsgBoxButton.One, CustomMsgBoxImage.Success);
                }
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            string s_date = History_StartDate1.Text;
            string e_date = History_EndDate1.Text;
            if (s_date == "" || e_date == "")
            {
                string CustomTitle = "日期选择异常";
                string CustomMsg = "日期范围选择不能为空！请确认！";
                if (MainWindow.LANGEUAGE != "zh-cn")
                {
                    CustomTitle = "Date selection exception";
                    CustomMsg = "Date range selection cannot be blank! Please confirm!";
                }
                CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Error);
                return;
            }
            string sql = "select * from RunData where 日期 >= '" + s_date + "' and 日期<='" + e_date + "'";
            if (RunDataTable != null)
            {
                RunDataTable.Clear();
            }
            RunDataTable = SqlHelper.GetDataSet(sql).Tables[0];
            dataGrid_log.ItemsSource = RunDataTable.DefaultView;
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            string CustomTitle = "权限不足";
            string CustomMsg = "用户:" + MainWindow.LoginUser + " 角色:" + MainWindow.LoginRole + "无权进行该操作";
            //判断是否管理员
            if (MainWindow.LoginUser != "admin")
            {
                if (MainWindow.LANGEUAGE != "zh-cn")
                {
                    CustomTitle = "Insufficient permissions";
                    CustomMsg = "User: " + MainWindow.LoginUser + " Role: " + MainWindow.LoginRole + " does not have permission to perform this operation";
                }
                CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Error);
                return;
            }
            CustomTitle = "警告";
            CustomMsg = "删除操作不可逆,是否确认要删除所选记录？";
            if (MainWindow.LANGEUAGE == "zh-cn")
            {
                if (CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Error).IsYes)
                {
                    string s_date = History_StartDate1.Text.Trim();
                    string e_date = History_EndDate1.Text.Trim();
                    if (string.IsNullOrWhiteSpace(s_date) || string.IsNullOrWhiteSpace(e_date))
                    {
                        CustomTitle = "错误";
                        CustomMsg = "日期范围选择不能为空！请确认！";
                        CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Error);
                        return;
                    }
                    string sql = "Delete RunData where 日期 >= '" + s_date + "' and 日期<='" + e_date + "'";
                    SqlHelper.Update(sql);
                    CustomTitle = "操作成功";
                    CustomMsg = "删除操作已成功执行！";
                    CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Success);
                    LoadDataGrid();
                }
            }
            else
            {
                CustomTitle = "Warning";
                CustomMsg = "Deletion is irreversible. Are you sure you want to delete the selected record?";
                if (CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Error).IsYes)
                {
                    string s_date = History_StartDate1.Text.Trim();
                    string e_date = History_EndDate1.Text.Trim();
                    if (string.IsNullOrWhiteSpace(s_date) || string.IsNullOrWhiteSpace(e_date))
                    {
                        CustomTitle = "Error";
                        CustomMsg = "Date range selection cannot be blank! Please confirm!";
                        CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Error);
                        return;
                    }
                    string sql = "Delete RunData where 日期 >= '" + s_date + "' and 日期<='" + e_date + "'";
                    SqlHelper.Update(sql);
                    CustomTitle = "Operation successful";
                    CustomMsg = "Delete operation has been successfully executed!";
                    CustomMsgBox.ShowDialog(CustomTitle, CustomMsg, CustomMsgBoxButton.One, CustomMsgBoxImage.Success);
                    LoadDataGrid();
                }
            }
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            SaveFileDialog exe = new SaveFileDialog();
            exe.Filter = " xlsx(*.xlsx)|*.xlsx| xls(*.xls)|*.xls";
            exe.FilterIndex = 0;
            exe.RestoreDirectory = true;
            //exe.CreatePrompt = true;
            exe.Title = "Export Excel File";
            exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            exe.FileName = string.Format("{0}_{1}", DateTime.Now.ToString("yyyy-MM-dd_HH时mm分ss秒"), "导出");
            bool exe_result = (bool)exe.ShowDialog();
            if (exe_result)
            {
                string path = exe.FileName;
                string result = Export.ToExcel(RunDataTable, path);
                if (result == "success")
                {
                    CustomMsgBox.ShowDialog("导出数据成功", "导出目录:" + path, CustomMsgBoxButton.One, CustomMsgBoxImage.Success);
                }
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //展示数据
            LoadDataGrid();
        }
    }
}
